package com.zhangxuehai.pdfviewer.ui;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.zhangxuehai.pdfviewer.R;

/**
 * @author 章学海
 * @date 2020/3/5
 */
public class PdfPenPickView extends ConstraintLayout {
    public interface PickCall{
        void onPick(@ColorInt int color, int width);
    }

    int[] colors = new int[]{Color.BLACK,Color.RED,Color.BLUE,Color.GREEN};
    int colorIndex = 0;
    int widthIndex = 0;

    ImageView penColorIndicator,penWidthIndicator;
    View black,red,blue,green,width1,width2,width3;

    PickCall pickCall;


    public PdfPenPickView(@NonNull Context context) {
        this(context,null);
    }

    public PdfPenPickView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setClickable(true);
        inflate(context,R.layout.view_pen_pick,this);

        penColorIndicator = findViewById(R.id.penColorIndicator);
        penWidthIndicator = findViewById(R.id.penWidthIndicator);
        black = findViewById(R.id.pen_black);
        red = findViewById(R.id.pen_red);
        blue = findViewById(R.id.pen_blue);
        green = findViewById(R.id.pen_green);
        black.setOnClickListener(colorClick);
        red.setOnClickListener(colorClick);
        blue.setOnClickListener(colorClick);
        green.setOnClickListener(colorClick);

        width1 = findViewById(R.id.pen_width1);
        width2 = findViewById(R.id.pen_width2);
        width3 = findViewById(R.id.pen_width3);
        width1.setOnClickListener(widthClick);
        width2.setOnClickListener(widthClick);
        width3.setOnClickListener(widthClick);
    }

    public void setPickCall(PickCall pickCall) {
        this.pickCall = pickCall;
    }

    OnClickListener colorClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            ConstraintLayout.LayoutParams lp = (LayoutParams) penColorIndicator.getLayoutParams();
            lp.leftToLeft = v.getId();
            lp.rightToRight = v.getId();
            penColorIndicator.setLayoutParams(lp);
            int id = v.getId();
            if(id == R.id.pen_black){
                colorIndex = 0;
            }else if(id == R.id.pen_red){
                colorIndex = 1;
            }else if(id == R.id.pen_blue){
                colorIndex = 2;
            }else if(id == R.id.pen_green){
                colorIndex = 3;
            }
            call();
        }
    };

    OnClickListener  widthClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            ConstraintLayout.LayoutParams lp = (LayoutParams) penWidthIndicator.getLayoutParams();
            lp.leftToLeft = v.getId();
            lp.rightToRight = v.getId();
            penWidthIndicator.setLayoutParams(lp);
            int id = v.getId();
            if(id == R.id.pen_width1){
                widthIndex = 0;
            }else if(id == R.id.pen_width2){
                widthIndex = 1;
            }else if(id == R.id.pen_width3){
                widthIndex = 2;
            }
            call();
        }
    };

    void call(){
        if(pickCall!=null){
            pickCall.onPick(colors[colorIndex],widthIndex);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if(visibility == VISIBLE){
            call();
        }
    }
}
